package com.thuanhoang.boo.adapter;

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.thuanhoang.boo.R;
import com.thuanhoang.boo.model.Feed;

public class FeedListAdapter extends BaseAdapter {
	private static ArrayList<Feed> feedList;
	private LayoutInflater layoutInflater;
	private Context context;

	public FeedListAdapter(Context context, ArrayList<Feed> feedList) {
		FeedListAdapter.feedList = feedList;
		this.layoutInflater = LayoutInflater.from(context);
		this.context = context;
		layoutInflater = LayoutInflater.from(context);
	}

	public Context getContext() {
		return context;
	}

	public int getCount() {
		return feedList.size();
	}

	public Object getItem(int position) {
		return feedList.get(position);
	}

	public long getItemId(int position) {
		return position;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		FeedViewHolder holder;
		Feed feed_item;
		if (convertView == null) {
			convertView = layoutInflater.inflate(R.layout.feed_item, parent,false);
			holder = new FeedViewHolder();
			holder.feedThumbnail = (ImageView) convertView.findViewById(R.id.feed_thumbnail);
			holder.storeImage = (ImageView) convertView.findViewById(R.id.store_image);
			holder.feedDescription =(TextView) convertView.findViewById(R.id.feed_description);

			convertView.setTag(holder);
		} else {
			holder = (FeedViewHolder) convertView.getTag();
		}
		try {
			feed_item = FeedListAdapter.feedList.get(position);
		} catch (Exception e) {
			return null;
		}

		holder.feedDescription.setText(feed_item.getFeedDesc());

		if (feed_item.getFeedImageLink()== ""
				|| feed_item.getFeedImageLink() == null) {

		} else {	
			ImageLoader.getInstance().displayImage(feed_item.getFeedImageLink(),
					holder.feedThumbnail);
		}
		if (feed_item.getStoreImageLink()== ""
				|| feed_item.getStoreImageLink() == null) {

		} else {	
			ImageLoader.getInstance().displayImage(feed_item.getStoreImageLink(),
					holder.storeImage);
		}
		
		
		return convertView;
	}

	public static class FeedViewHolder {
		ImageView feedThumbnail;
		ImageView storeImage;
		TextView feedDescription;

	}
}
